/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */

//第一种解法,使用ArrayList和数组.
class Solution {
    public int[] reversePrint(ListNode head) {
        ListNode cur = head;
        List<Integer> list = new ArrayList<>();
        while(cur != null){
            list.add(cur.val);
            cur = cur.next;
        }
        Collections.reverse(list);
        int[] arr = new int[list.size()];
        for(int i = 0; i < list.size() ; i++){
            arr[i] = list.get(i);
        }
        return arr;
    }
}

//第二种解法,使用栈和数组
class Solution {
    public int[] reversePrint(ListNode head) {
        ListNode cur = head;
        Stack<Integer> stack = new Stack<>();
        while(cur != null){
            stack.push(cur.val);
            cur = cur.next;
        }
        int size = stack.size();
        int[] arr = new int[size];
        for(int i = 0; i < size; i++){
            arr[i] = stack.pop();
        }
        return arr;
    }
}